package com.xhwy.util;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 * @param <T> 存储集合的泛型
 */
public class PageUtil<T> implements Serializable {
    private String code; //状态码
    private Integer currentPage;//当前页
    private Integer pageSize;//每页记录数
    private Integer totalPages;//总页数
    private Integer totalRecords;//总记录数
    private List<T> list;//查询到的数据集合
    private Integer offset;//查询时的便宜量
    private T t;

    public Integer getCurrentPage() {
        return currentPage;
    }
    //设置当前页
    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    //设置每页记录数
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPages() {
        return totalPages;
    }
    //设置总页数
    public void setTotalPages() {
        this.totalPages = this.totalRecords % this.pageSize == 0? this.totalRecords / this.pageSize : this.totalRecords / this.pageSize + 1;
    }

    public Integer getTotalRecords() {
        return totalRecords;
    }
    //设置总记录数
    public void setTotalRecords(Integer totalRecords) {
        if (totalRecords >= 0){
            this.totalRecords = totalRecords;
        }else {
            this.totalRecords = 0;
        }
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }

    //获取查询时的偏移量
    public Integer getOffset() {
        if(currentPage == null || pageSize == null) return this.offset = null;
        if(!(currentPage > 0 && pageSize > 0)){
            //设置默认页数和每页记录数
            this.setCurrentPage(1);
            this.setPageSize(10);
        }
        return this.offset = (this.currentPage-1) * pageSize;
    }


    //是否有上一页
//    public boolean isPrePage(){
//        if (this.currentPage > 1){
//            return false;
//        }
//        return false;
//    }
    //是否有下一页
//    public boolean isNextPage(){
//        if(this.currentPage < this.totalPages){
//            return true;
//        }
//        return false;
//    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "PageUtil{" +
                "currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", totalPages=" + totalPages +
                ", totalRecords=" + totalRecords +
                ", list=" + list +
                ", t=" + t +
                '}';
    }
}
